﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Reflection;
using DevExpress.Xpf.Bars;
using Cinser.Common.Wpf.Helpers;
using DevExpress.Xpf.Docking;
using DevExpress.Xpf.NavBar;
using Cinser.Common.Resources;

namespace Cinser.Common.Wpf.Helpers
{
    public static class ImagesHelperWPF
    {
        #region Dependency Properties
        public static readonly DependencyProperty ImageProperty;
        static ImagesHelperWPF()
        {
            Type ownerType = typeof(ImagesHelperWPF);
            ImageProperty = DependencyProperty.RegisterAttached("Image", typeof(string), ownerType, new PropertyMetadata(null, RaiseVideoRentImageChanged));
        }
        #endregion

        public static string GetImage(DependencyObject d) { return (string)d.GetValue(ImageProperty); }
        public static void SetImage(DependencyObject d, string value) { d.SetValue(ImageProperty, value); }

        static void RaiseVideoRentImageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is Image)
            {
                SetImageToImage((Image)d, (string)e.NewValue);
            }
            if (d is BarButtonItem)
            {
                SetImageToBarButtonItemGlyph((BarButtonItem)d, (string)e.NewValue);
                return;
            }

            if (d is LayoutPanel)
            {
                SetImageToLayoutPanelCaptionImage((LayoutPanel)d, (string)e.NewValue);
                return;
            }

            //NavBarGroup
            if (d is NavBarGroup)
            {
                SetImageToNavBarGroupImageSource((NavBarGroup)d, (string)e.NewValue);
                return;
            }
            //NavBarItem
            if (d is NavBarItem)
            {
                SetImageToNavBarItemImageSource((NavBarItem)d, (string)e.NewValue);
                return;
            }
            //DocumentPanel
            if (d is DocumentPanel)
            {
                SetImageToDocumentPanelCaptionImage((DocumentPanel)d, (string)e.NewValue);
                return;
            }
        }

        private static void SetImageToDocumentPanelCaptionImage(DocumentPanel documentPanel, string p)
        {
            documentPanel.CaptionImage = GetImageSource(p + "_16x16");
        }

        private static void SetImageToNavBarItemImageSource(NavBarItem navBarItem, string p)
        {
            navBarItem.ImageSource = GetImageSource(p + "_16x16");
        }

        private static void SetImageToNavBarGroupImageSource(NavBarGroup navBarGroup, string p)
        {
            navBarGroup.ImageSource = GetImageSource(p + "_32x32");
        }

        private static void SetImageToLayoutPanelCaptionImage(LayoutPanel layoutPanel, string imageName)
        {
            layoutPanel.CaptionImage = GetImageSource(imageName + "_16x16");
        }

        static void SetImageToImage(Image image, string imageName)
        {
            image.Source = GetImageSource(imageName);
        }
        static void SetImageToBarButtonItemGlyph(BarButtonItem bbi, string imageName)
        {
            bbi.LargeGlyph = GetImageSource(imageName + "_32x32");
            bbi.Glyph = GetImageSource(imageName + "_16x16");
        }

        static ImageSource GetImageSource(string imageName)
        {
            Assembly assembly = typeof(Cinser.Common.Resources.ImagesHelper).Assembly;
            return ImageSourceHelper.CreateImageFromEmbeddedResource(assembly, string.Format("Cinser.Common.Resources.SingleImages.{0}.png", imageName), true);
        }
    }
}
